package com.web.server.common.response;

import com.google.common.base.MoreObjects;

import java.util.LinkedList;
import java.util.List;

/**
 * 分页数据
 */
public class Page<T> {
    private static final int DEFAULT_PAGE_SIZE = 10;
    private static final int DEFAULT_PAGE_NO = 1;

    protected List<T> data = new LinkedList<T>();    // 数据对象
    protected int pageNo = DEFAULT_PAGE_NO;    // 当前页码，默认值为：1
    protected int pageSize = DEFAULT_PAGE_SIZE;   // 每页记录条数，默认值：10
    protected int totalCount = 0;    // 总记录条数

    public Page() {

    }

    public Page(List<T> data, int pageNo) {
        this.data = data;
        this.pageNo = pageNo;
    }

    public Page(List<T> data, int totalCount, int pageNo) {
        this.data = data;
        this.pageNo = pageNo;
        this.pageSize = pageSize == -1 ? totalCount : pageSize;
        this.totalCount = totalCount;
    }

    public Page(List<T> data, int totalCount, int pageSize, int pageNo) {
        this.data = data;
        this.pageNo = pageNo;
        this.pageSize = pageSize == -1 ? totalCount : pageSize;
        this.totalCount = totalCount;
    }

    public int getPageNo() {
        return pageNo>0?pageNo:DEFAULT_PAGE_NO;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize>0?pageSize:DEFAULT_PAGE_SIZE;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageCount() { //总页数
        return (totalCount % this.getPageSize()) == 0 ? (totalCount / this.getPageSize()) : (totalCount / this.getPageSize() + 1);
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public  int getRowStart(){
        return (this.getPageNo()-1)*this.getPageSize();
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("pageNo", pageNo)
                .add("pageSize", pageSize)
                .add("totalCount", totalCount)
                .add("pageCount", this.getPageCount())
                .toString();
    }
}
